package com.ddstation.common.service;

import com.ddstation.common.mapper.DtInvsItemInstitutionMapper;
import com.ddstation.common.model.InvsItemInstitutionModel;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service @Lazy
public class InvsItemInstitutionServiceImpl implements InvsItemInstitutionService {

	@Inject private DtInvsItemInstitutionMapper dtInvsItemInstitutionMapper;

	@Override
	public List<InvsItemInstitutionModel> getInvsItemInstitutionList(Integer catalogId, String institutionId) {
		return dtInvsItemInstitutionMapper.searchInvsItemList(catalogId,institutionId);
	}

	@Override
	public int selectInvsItemAllCnt(Integer catalogId, String institutionId) {
		return dtInvsItemInstitutionMapper.selectInvsItemAllCnt(catalogId,institutionId);
	}

	@Override
	public void init(Integer catalogId, String institutionId) {
		dtInvsItemInstitutionMapper.initData(catalogId,institutionId);
	}

	@Override
	public List<InvsItemInstitutionModel> selectShowInvsItem(Integer catalogId, String institutionId) {
		return dtInvsItemInstitutionMapper.selectShowInvsItem(catalogId,institutionId);
	}

	@Override
	public int addInvsItem(Integer catalogId, String institutionId, String name) {
		return this.addInvsItem(catalogId,institutionId,name,null);
	}

	@Override
	public int addInvsItem(Integer catalogId, String institutionId, String name,String comment) {
		return dtInvsItemInstitutionMapper.insertInvsItem(catalogId,institutionId,name,comment);
	}

	@Override
	public List<String> checkByInvsItemName(Integer catalogId, String institutionId, String name) {
		return dtInvsItemInstitutionMapper.checkByInvsItemName(catalogId,institutionId,name);
	}

	@Override
	public int delInvsItem(Integer catalogId, String institutionId, Integer id) {
		return dtInvsItemInstitutionMapper.deleteInvsItem(catalogId,institutionId,id);
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public void saveInvsItem(Integer catalogId, String institutionId, String ids) {
		List<String> idStrList = Arrays.asList(ids.split(","));
		List<Integer> idList = new ArrayList<Integer>();
		for (String idStr : idStrList) {
			if (idStr != null && idStr.length()>0) {
				idList.add(Integer.parseInt(idStr));
			}
		}
		dtInvsItemInstitutionMapper.disabledInvsItem(catalogId,institutionId,idList);
		dtInvsItemInstitutionMapper.enableInvsItem(catalogId,institutionId,idList);
	}
}
